package com.hm.health.entity.arguments;

import android.content.Context;
import android.os.Bundle;

import com.goodix.libromablecom.sdk.AlgorithmSceneType;
import com.goodix.libromablecom.sdk.cardiff.arguments.StartArgumentsKeyEnum;
import com.goodix.utils.DefaultSpUtil;
import com.goodix.utils.SPUtils;
import com.hm.health.MyApplication;
import com.hm.health.R;

import static com.goodix.libromablecom.sdk.cardiff.arguments.StartArgumentsKeyEnum.CONTINUOUS_MEASUREMENT;

/**
 * Created by chenshi on 2020/2/24.
 */

public class StartArgumentsManager {

    public static final String KEY_MEASURE_FUNCTION_NAME = "perfs_key_measure_function_name";
    public static final String ARG_CACHE_HEADER = "ARG_CACHE_HEADER_";

    // 场景名称
    private String mSceneName = "默认";
    // 算法策略
    private int mAlgCalculateStrategy;
    // 应用场景 {0：连续测量；1：单次测量}
    private int mMeasurementMode = CONTINUOUS_MEASUREMENT;
    // 心电贴标识符
    private int mTestId = 0;

    private static StartArgumentsManager instance;

    public static synchronized StartArgumentsManager getInstance() {
        if (null == instance) {
            instance = new StartArgumentsManager();
        }
        return instance;
    }

    public Bundle getBundle() {
        Bundle bundle = new Bundle();

        // 使能佩戴检测
        int enableWearDetect = MyApplication.getHbdOptionIni().isEnableWearDetection() ? 1 : 0;
        bundle.putInt(StartArgumentsKeyEnum.ARG_ENABLE_WEARING_DETECT, enableWearDetect);

        // 使能SNR计算
        bundle.putInt(StartArgumentsKeyEnum.ARG_ENABLE_SNR_CALCULATE, 1);

        // 计算策略
        bundle.putInt(StartArgumentsKeyEnum.ARG_ALGORITHM_CALCULATE_STRATEGY,
                mAlgCalculateStrategy);

        // 功能名称
        bundle.putString(StartArgumentsKeyEnum.ARG_MEASURE_FUNCTION_NAME, getFunctionName());

        // 场景ID
        int sceneId = getAlgorithmScene(mSceneName, MyApplication.getContext());
        bundle.putInt(StartArgumentsKeyEnum.ARG_ALGORITHM_SCENE_ID, sceneId);

        // 测量模式
        bundle.putInt(StartArgumentsKeyEnum.ALG_ECG_CLASSIFICATION_MODE, mMeasurementMode);

        // 随机test id
        mTestId = (int)(System.currentTimeMillis() / 1000);
        bundle.putInt(StartArgumentsKeyEnum.ALG_ECG_TEST_ID, mTestId);

        return bundle;
    }

    public void cacheBundle(Context context, Bundle bundle) {
        SPUtils.put(context, ARG_CACHE_HEADER + StartArgumentsKeyEnum.ARG_ENABLE_WEARING_DETECT,
                bundle.getInt(StartArgumentsKeyEnum.ARG_ENABLE_WEARING_DETECT));
        SPUtils.put(context, ARG_CACHE_HEADER + StartArgumentsKeyEnum.ARG_ENABLE_SNR_CALCULATE,
                bundle.getInt(StartArgumentsKeyEnum.ARG_ENABLE_SNR_CALCULATE));
        SPUtils.put(context, ARG_CACHE_HEADER + StartArgumentsKeyEnum.ARG_ALGORITHM_CALCULATE_STRATEGY,
                bundle.getInt(StartArgumentsKeyEnum.ARG_ALGORITHM_CALCULATE_STRATEGY));
        SPUtils.put(context, ARG_CACHE_HEADER + StartArgumentsKeyEnum.ARG_MEASURE_FUNCTION_NAME,
                bundle.getString(StartArgumentsKeyEnum.ARG_MEASURE_FUNCTION_NAME));
        SPUtils.put(context, ARG_CACHE_HEADER + StartArgumentsKeyEnum.ARG_ALGORITHM_SCENE_ID,
                bundle.getInt(StartArgumentsKeyEnum.ARG_ALGORITHM_SCENE_ID));
        SPUtils.put(context, ARG_CACHE_HEADER + StartArgumentsKeyEnum.ALG_ECG_CLASSIFICATION_MODE,
                bundle.getInt(StartArgumentsKeyEnum.ALG_ECG_CLASSIFICATION_MODE));
        SPUtils.put(context, ARG_CACHE_HEADER + StartArgumentsKeyEnum.ALG_ECG_TEST_ID,
                bundle.getInt(StartArgumentsKeyEnum.ALG_ECG_TEST_ID));
    }

    public Bundle getRecoverBundle(Context context) {
        Bundle bundle = new Bundle();
        bundle.putInt(StartArgumentsKeyEnum.ARG_ENABLE_WEARING_DETECT,
                (int)SPUtils.get(context, ARG_CACHE_HEADER + StartArgumentsKeyEnum.ARG_ENABLE_WEARING_DETECT, 0));
        bundle.putInt(StartArgumentsKeyEnum.ARG_ENABLE_SNR_CALCULATE,
                (int)SPUtils.get(context, ARG_CACHE_HEADER + StartArgumentsKeyEnum.ARG_ENABLE_SNR_CALCULATE, 1));
        bundle.putInt(StartArgumentsKeyEnum.ARG_ALGORITHM_CALCULATE_STRATEGY,
                (int)SPUtils.get(context, ARG_CACHE_HEADER + StartArgumentsKeyEnum.ARG_ALGORITHM_CALCULATE_STRATEGY, mAlgCalculateStrategy));
        bundle.putString(StartArgumentsKeyEnum.ARG_MEASURE_FUNCTION_NAME,
                (String)SPUtils.get(context, ARG_CACHE_HEADER + StartArgumentsKeyEnum.ARG_MEASURE_FUNCTION_NAME, getFunctionName()));
        bundle.putInt(StartArgumentsKeyEnum.ARG_ALGORITHM_SCENE_ID,
                (int)SPUtils.get(context, ARG_CACHE_HEADER + StartArgumentsKeyEnum.ARG_ALGORITHM_SCENE_ID, 0));
        bundle.putInt(StartArgumentsKeyEnum.ALG_ECG_CLASSIFICATION_MODE,
                (int)SPUtils.get(context, ARG_CACHE_HEADER + StartArgumentsKeyEnum.ALG_ECG_CLASSIFICATION_MODE, mMeasurementMode));
        int cacheSceneId = (int)SPUtils.get(context, ARG_CACHE_HEADER + StartArgumentsKeyEnum.ARG_ALGORITHM_SCENE_ID, 0);
        mSceneName = getSceneName(cacheSceneId, context);

        mTestId = (int)SPUtils.get(context, ARG_CACHE_HEADER + StartArgumentsKeyEnum.ALG_ECG_TEST_ID, mTestId);
        bundle.putInt(StartArgumentsKeyEnum.ALG_ECG_TEST_ID, mTestId);

        return bundle;
    }

    public String getFunctionName() {
        return DefaultSpUtil.getValueWithDefault(MyApplication.getContext(),
                KEY_MEASURE_FUNCTION_NAME, StartArgumentsKeyEnum.AppFunction_HR);
    }

    public void setFunctionName(String functionName) {
        DefaultSpUtil.put(MyApplication.getContext(),
                KEY_MEASURE_FUNCTION_NAME, functionName);
    }

    public void setAlgCalculateStrategy(int algCalculateStrategy) {
        mAlgCalculateStrategy = algCalculateStrategy;
    }

    public void setMeasurementMode(int measurementMode) {
        mMeasurementMode = measurementMode;
    }

    public void setSceneName(String sceneName) {
        mSceneName = sceneName;
    }

    public String getSceneName() {
        return mSceneName;
    }

    public int getTestId() {
        return mTestId;
    }

    /**
     * 设置算法场景类型
     * @param sceneName 场景名称
     *
     */
    public static int getAlgorithmScene(String sceneName, Context context) {
        if (sceneName.equals(context.getString(R.string.scenes_of_default))) {
            return AlgorithmSceneType.HBA_SCENES_DEFAULT;
        } else if(sceneName.equals(context.getString(R.string.scenes_of_life))) {
            return AlgorithmSceneType.HBA_SCENES_DAILY_LIFE;
        } else if(sceneName.equals(context.getString(R.string.running_machine))) {
            return AlgorithmSceneType.HBA_SCENES_RUNNING_INSIDE;
        } else if(sceneName.equals(context.getString(R.string.climb_stairs))) {
            return AlgorithmSceneType.HBA_SCENES_STAIRS;
        } else if (sceneName.equals(context.getString(R.string.running_machine))) {
            return AlgorithmSceneType.HBA_SCENES_WALKING_INSIDE;
        } else if (sceneName.equals(context.getString(R.string.jogging_outdoor))) {
            return AlgorithmSceneType.HBA_SCENES_RUNNING_OUTSIDE;
        } else if (sceneName.equals(context.getString(R.string.walking_outdoor))) {
            return AlgorithmSceneType.HBA_SCENES_WALKING_OUTSIDE;
        } else if(sceneName.equals(context.getString(R.string.motionless))) {
            return AlgorithmSceneType.HBA_SCENES_STILL_REST;
        } else if (sceneName.equals(context.getString(R.string.rest))) {
            return AlgorithmSceneType.HBA_SCENES_REST;
        } else if(sceneName.equals(context.getString(R.string.hold_breath))) {
            return AlgorithmSceneType.HBA_SCENES_STILLRADON;
        } else if(sceneName.equals(context.getString(R.string.cycling_indoor))) {
            return AlgorithmSceneType.HBA_SCENES_BIKING_INSIDE;
        } else if(sceneName.equals(context.getString(R.string.cycling_outdoor))) {
            return AlgorithmSceneType.HBA_SCENES_BIKING_OUTSIDE;
        } else if(sceneName.equals(context.getString(R.string.cycling_cross_country_outdoor))) {
            return AlgorithmSceneType.HBA_SCENES_BIKING_MOUNTAIN;
        } else if(sceneName.equals(context.getString(R.string.running_with_high_heart_rate))) {
            return AlgorithmSceneType.HBA_SCENES_RUNNING_HIGH_HR;
        } else if(sceneName.equals(context.getString(R.string.running_machine_combined_running))) {
            return AlgorithmSceneType.HBA_SCENES_RUNNING_TREADMILL_CCOMBINE;
        } else if(sceneName.equals(context.getString(R.string.high_duty_combined_sports))) {
            return AlgorithmSceneType.HBA_SCENES_HIGH_INTENSITY_COMBINE;
        } else if(sceneName.equals(context.getString(R.string.combined_traditional_strength_training))) {
            return AlgorithmSceneType.HBA_SCENES_TRADITIONAL_STRENGTH_COMBINE;
        } else if(sceneName.equals(context.getString(R.string.test_with_stairs))) {
            return AlgorithmSceneType.HBA_SCENES_STEP_TEST;
        } else if(sceneName.equals(context.getString(R.string.ball_sports))) {
            return AlgorithmSceneType.HBA_SCENES_BALL_SPORTS;
        } else if(sceneName.equals(context.getString(R.string.aerobics))) {
            return AlgorithmSceneType.HBA_SCENES_AEROBICS;
        } else {
            return AlgorithmSceneType.HBA_SCENES_DEFAULT;
        }
    }

    public static String getSceneName(int sceneID, Context context) {
        if (AlgorithmSceneType.HBA_SCENES_DEFAULT == sceneID) {
            return context.getString(R.string.scenes_of_default);
        } else if(AlgorithmSceneType.HBA_SCENES_DAILY_LIFE == sceneID) {
            return context.getString(R.string.scenes_of_life);
        } else if(AlgorithmSceneType.HBA_SCENES_RUNNING_INSIDE == sceneID) {
            return context.getString(R.string.running_machine);
        } else if(AlgorithmSceneType.HBA_SCENES_STAIRS == sceneID) {
            return context.getString(R.string.climb_stairs);
        } else if (AlgorithmSceneType.HBA_SCENES_WALKING_INSIDE == sceneID) {
            return context.getString(R.string.running_machine);
        } else if (AlgorithmSceneType.HBA_SCENES_RUNNING_OUTSIDE == sceneID) {
            return context.getString(R.string.jogging_outdoor);
        } else if (AlgorithmSceneType.HBA_SCENES_WALKING_OUTSIDE == sceneID) {
            return context.getString(R.string.walking_outdoor);
        } else if(AlgorithmSceneType.HBA_SCENES_STILL_REST == sceneID) {
            return context.getString(R.string.motionless);
        } else if (AlgorithmSceneType.HBA_SCENES_REST == sceneID) {
            return context.getString(R.string.rest);
        } else if(AlgorithmSceneType.HBA_SCENES_STILLRADON == sceneID) {
            return context.getString(R.string.hold_breath);
        } else if(AlgorithmSceneType.HBA_SCENES_BIKING_INSIDE == sceneID) {
            return context.getString(R.string.cycling_indoor);
        } else if(AlgorithmSceneType.HBA_SCENES_BIKING_OUTSIDE == sceneID) {
            return context.getString(R.string.cycling_outdoor);
        } else if(AlgorithmSceneType.HBA_SCENES_BIKING_MOUNTAIN == sceneID) {
            return context.getString(R.string.cycling_cross_country_outdoor);
        } else if(AlgorithmSceneType.HBA_SCENES_RUNNING_HIGH_HR == sceneID) {
            return context.getString(R.string.running_with_high_heart_rate);
        } else if(AlgorithmSceneType.HBA_SCENES_RUNNING_TREADMILL_CCOMBINE == sceneID) {
            return context.getString(R.string.running_machine_combined_running);
        } else if(AlgorithmSceneType.HBA_SCENES_HIGH_INTENSITY_COMBINE == sceneID) {
            return context.getString(R.string.high_duty_combined_sports);
        } else if(AlgorithmSceneType.HBA_SCENES_TRADITIONAL_STRENGTH_COMBINE == sceneID) {
            return context.getString(R.string.combined_traditional_strength_training);
        } else if(AlgorithmSceneType.HBA_SCENES_STEP_TEST == sceneID) {
            return context.getString(R.string.test_with_stairs);
        } else if(AlgorithmSceneType.HBA_SCENES_BALL_SPORTS == sceneID) {
            return context.getString(R.string.ball_sports);
        } else if(AlgorithmSceneType.HBA_SCENES_AEROBICS == sceneID) {
            return context.getString(R.string.aerobics);
        } else {
            return context.getString(R.string.scenes_of_default);
        }
    }

    public int getSceneId() {
        return getAlgorithmScene(mSceneName, MyApplication.getContext());
    }

    public int getMeasurementMode() {
        return mMeasurementMode;
    }
}
